﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Globalization;
using System.Threading;
using System.Windows.Forms;
using HotelSoft.HotelManagement_BUS;
using HotelSoft.HotelManagement_DTO;

namespace HotelSoft.HotelManagement_GUI
{
    public partial class frmDat_PHONG : Form
    {
        PhongBUS busPhong = new PhongBUS();
        PhieuDangKyDTO dtoPhieuDangKy = new PhieuDangKyDTO();
        Double temp = 1;
        frmQuanly_PHIEUDK frmPHIEUDANGKY;
        private static string Sophong;
        private static int Songaythue;
        private static DateTime Ngaydangky;
        private static string Sochungminhnhandan;
        private static string soCMND;
        private static bool checkCreatePDK = false;
        private static string phong;

        public string PHONG
        {
            get { return phong; }
            set { phong = value; }
        }
        public bool CHECKCREATE_PDK
        {
            get { return checkCreatePDK; }
            set { checkCreatePDK = value; }
        }
        
        public string SOCMND
        {
            get { return soCMND; }
            set { soCMND = value; }
        }

        public frmDat_PHONG()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void frmDat_PHONG_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            CultureInfo culture = (CultureInfo)CultureInfo.CurrentCulture.Clone();
            culture.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            Thread.CurrentThread.CurrentCulture = culture;
            lblSophong.Text = PHONG;
            dtoPhieuDangKy.Ngayden = DateTime.Now;
            dtbNgayBatDauThue.Text = dtoPhieuDangKy.Ngayden.ToString();
            Ngaydangky = Convert.ToDateTime(dtbNgayBatDauThue.Text.ToString());
            txtCMND.Enabled = false;
        }

        private void btnPHIEUDANGKY_Click(object sender, EventArgs e)
        {
            frmPHIEUDANGKY = new frmQuanly_PHIEUDK();
            frmPHIEUDANGKY.Sophong = lblSophong.Text;
            frmPHIEUDANGKY.Songaythue = int.Parse(txtSoNgayThue.Text);
            frmPHIEUDANGKY.Ngaythue = dtbNgayBatDauThue.Value;
            frmPHIEUDANGKY.NEWPDK = checkCreatePDK;
            this.Hide();
            frmPHIEUDANGKY.Show();
            frmPHIEUDANGKY.FormClosing += new FormClosingEventHandler(frmPHIEUDANGKY_FormClosing);
            txtCMND.Enabled = true;
            txtCMND.Focus();
        }

        void frmPHIEUDANGKY_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Show();
        }

        private void txtSoNgayThue_Validating(object sender, CancelEventArgs e)
        {
            Songaythue = int.Parse(txtSoNgayThue.Text.ToString());
        }

        private void dtbNgayBatDauThue_Validating(object sender, CancelEventArgs e)
        {
            Ngaydangky = Convert.ToDateTime(dtbNgayBatDauThue.Text.ToString());
        }

        private void btnTrangThai_Click(object sender, EventArgs e)
        {
            if (checkCreatePDK == false)
            {
                MessageBox.Show("Xin hãy lập phiếu đăng ký!", "Thông báo: Đặt phòng");
                return;
            }
            if (lbxKhachhang.Items.Count == 0)
            {
                MessageBox.Show("Xin hãy nhập số CMND của khách hàng","Thông báo: Đặt phòng");
                return;
            }
            DialogResult kq = MessageBox.Show("Xác nhận thuê phòng!", "Xác nhận", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (kq == DialogResult.Yes)
            {
                KhachHangBUS busKhachHang = new KhachHangBUS();
                DataTable tb = new DataTable();
                tb = busKhachHang.laySoPDK();
                string sophieu = tb.Rows[0].ItemArray[0].ToString();
                for (int i = 0; i < lbxKhachhang.Items.Count; i++)
                {
                    KhachHang_PhieuDangKyDTO dtokh_pdk = new KhachHang_PhieuDangKyDTO();
                    dtokh_pdk.CMND = lbxKhachhang.Items[i].ToString();
                    dtokh_pdk.SoPDK = sophieu;
                    busKhachHang.ThemKHACHHANG_PHIEUDANGKY(dtokh_pdk);
                }

                PhongDTO dto = new PhongDTO();
                dto.SOPHONG = lblSophong.Text;
                busPhong.tablePhongdondulieus(dto);
                btnTrangThai.Enabled = false;
                Btn_Thoat.Focus();
            }
        }

        private void btnAddKH_Click(object sender, EventArgs e)
        {
            if (txtCMND.Text != "")
            {

                KhachHangDTO dto = new KhachHangDTO();
                dto.CMND = txtCMND.Text.ToString();
                // MessageBox.Show(dto.CMND, "thong bao");
                KhachHangBUS busKHACHHANG = new KhachHangBUS();
                int dem;
                dem = busKHACHHANG.Timkhachhang(dto);
                //MessageBox.Show(dem.ToString(), "thong bao");
                if (dem > 0)
                {
                    lbxKhachhang.Items.Add(txtCMND.Text);
                }
                else
                {
                    frmQuanly_KHACHHANG frmKHACHHANG = new frmQuanly_KHACHHANG();
                    Sochungminhnhandan = txtCMND.Text.ToString();
                    frmKHACHHANG.Chungminhnhandan = Sochungminhnhandan;
                    frmKHACHHANG.ShowDialog();
                    txtCMND.Text = "";
                    txtCMND.Focus();
                    if (soCMND != null)
                        lbxKhachhang.Items.Add(soCMND);
                }
            }
        }

        private void Btn_Thoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Opacity = this.Opacity + 0.1;
            if (this.Opacity == temp)
                timer1.Enabled = false;
        }
    }
}
